package xin.ssm.jxc.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import xin.ssm.jxc.model.Employees;
import xin.ssm.jxc.services.IEmployeeService;
import xin.ssm.jxc.util.Result;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
	@Resource
	private IEmployeeService employeeService;
	
	// 查询所有员工
		@RequestMapping("/getallemployee")
		@ResponseBody
		public Result findAllEmployee() {
			List<Employees> userList = employeeService.findAllEmployee();
			// 时间转换
			String jsonarry = JSONArray.toJSONStringWithDateFormat(userList, "yyyy-MM-dd");
			Result result = new Result();
			result.setCode(0);
			result.setMsg("成功");
			result.setCount(userList.size());
			result.setData(userList);
			return result;
		}
		//页面查询准备数据
		@RequestMapping("/findallemployee")
		@ResponseBody
		public List<Employees> findEmployee() {
			List<Employees> employeelist = employeeService.findAllEmployee();
			return employeelist;
		}
		
		// 动态查询所有员工
				@RequestMapping("/findemployeedynamic")
				@ResponseBody
				public Result findUserDynamic(
						@RequestParam(value = "employeecode") String employeecode,
						@RequestParam(value = "employeename") String employeename,
						@RequestParam(value = "employeesex") String employeesex,
						@RequestParam(value = "employeeemail") String employeeemail) {
					if(employeecode.equals("") || employeecode.isEmpty()) {
						employeecode=null;
					}
					if(employeename.equals("") || employeename.isEmpty()) {
						employeename=null;
					}
					if(employeesex.equals("") || employeesex.isEmpty()) {
						employeesex=null;
					}
					if(employeeemail.equals("") || employeeemail.isEmpty()) {
						employeeemail=null;
					}
					Employees e = new Employees();
					e.setEmployeecode(employeecode);
					e.setEmployeename(employeename);
					e.setEmployeesex(employeesex);
					e.setEmployeeemail(employeeemail);
					List<Employees> userList = employeeService.findEmployeeDynamic(e);
					// 时间转换
					String jsonarry = JSONArray.toJSONStringWithDateFormat(userList, "yyyy-MM-dd");
					Result result = new Result();
					result.setCode(0);
					result.setMsg("成功");
					result.setCount(userList.size());
					result.setData(userList);
					return result;
				}
		// 删除员工
		@RequestMapping("/deleteemployee")
		@ResponseBody
		public String deleteUsers(@RequestParam(value = "employeecode") String employeecode) {
			int ret = employeeService.deleteEmployee(employeecode);
			if (ret > 0) {
				return "1";
			} else {
				return "0";
			}
		}
		
		// 修改员工
		@RequestMapping("/updateemployee")
		@ResponseBody
		public String updateUsers(
				@RequestParam(value = "employeecode") String employeecode,
				@RequestParam(value = "employeename") String employeename,
				@RequestParam(value = "employeebirthday") String employeebirthday,//直接存日期少一天
				@RequestParam(value = "employeesex") String employeesex,
				@RequestParam(value = "employeetel") String employeetel,
				@RequestParam(value = "employeeemail") String employeeemail) {
			Date reldate = new Date();
			Employees e = new Employees();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = sdf.parse(employeebirthday);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.DAY_OF_MONTH, 1);//加一天
				reldate=calendar.getTime();
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.setEmployeecode(employeecode);
			e.setEmployeename(employeename);
			e.setEmployeebirthday(reldate);
			e.setEmployeesex(employeesex);
			e.setEmployeetel(employeetel);
			e.setEmployeeemail(employeeemail);
			int ret = employeeService.updateEmployee(e);
			if (ret > 0) {
				return "1";
			} else {
				return "0";
			}
		}
		
		// 管理员添加员工
				@RequestMapping("/addemployee")
				@ResponseBody
				public String AdminAddEmployee(
						@RequestParam(value = "employeecode") String employeecode,
						@RequestParam(value = "employeename") String employeename,
						@RequestParam(value = "employeebirthday") String employeebirthday,
						@RequestParam(value = "employeesex") String employeesex,
						@RequestParam(value = "employeetel") String employeetel,
						@RequestParam(value = "employeeemail") String employeeemail) {
					System.out.println("性别："+employeesex);
					Date reldate = new Date();
					Employees e = new Employees();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					try {
						Date date = sdf.parse(employeebirthday);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(date);
						calendar.add(Calendar.DAY_OF_MONTH, 1);//加一天
						reldate=calendar.getTime();
					} catch (ParseException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					e.setEmployeecode(employeecode);
					e.setEmployeename(employeename);
					e.setEmployeebirthday(reldate);
					e.setEmployeesex(employeesex);
					e.setEmployeetel(employeetel);
					e.setEmployeeemail(employeeemail);
					int ret = employeeService.AddEmployee(e);
					if (ret > 0) {
						return "1";
					} else {
						return "0";
					}
				}
}
